package com.probiz.estore.sales.model.condition;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.probiz.estore.common.model.cart.Shoppingcart;
import com.probiz.estore.common.model.cart.ShoppingcartItem;

/**
 * CartSubtotalCondition cart subTotal(standardTotoalPrice) is at least
 * $XX(key=SUBTOTAL_AMOUNT)
 * 
 * @author ProBIZ
 * 
 */
public class CartTotalCondition extends CartAbstractCondition {
	private static final Log logger = LogFactory.getLog(CartTotalCondition.class);
	public static final String TOTAL_AMOUNT = "TOTAL_AMOUNT";
	private BigDecimal paramTotalAmount;
	private Map<String, String> params;

	public CartTotalCondition(Map<String, String> _params) {
		this.paramTotalAmount = new BigDecimal( _params.get(TOTAL_AMOUNT));
		this.params = _params;
	}
	public Map<String, String> getParams()
	{
		return params;
	}
	
	public boolean getResult(Shoppingcart _cart) {
		logger.debug(new StringBuffer().append("[TOTAL_AMOUNT|").append(paramTotalAmount).append("]~~{").append(_cart.getTotal()).append("}").toString());

		// -----------用于特价不能折上折
		//先算一个临时的去掉所有特价的商品的购物车总价  
		BigDecimal amountWithSalesAmount = new BigDecimal("0");
		for(ShoppingcartItem item : _cart.getShoppingCartItems()){
			if(item.getIsOnSale().shortValue() == ShoppingcartItem.ISONSALE_YES){
				amountWithSalesAmount = amountWithSalesAmount.add(item.getDiscountPrice().multiply(new BigDecimal(item.getDiscountQuantity())));
			}
		}
		if(amountWithSalesAmount.compareTo(_cart.getTotal()) <= 0){
			return ((paramTotalAmount.compareTo(_cart.getTotal().add(amountWithSalesAmount.negate()))) == 1) ? false
					: true;
		}else{
			return ((paramTotalAmount.compareTo(_cart.getTotal())) == 1) ? false
					: true;
		}
		

	}

}
